package com.swgk.core.util;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.text.TextUtils;
import android.view.DisplayCutout;
import android.view.View;
import android.view.WindowInsets;

import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by WYJ on 2020-11-05.
 */
public class LiuHaiPingUtils {

    private static DisplayCutout displayCutout;

    /**
     * 判断是否有刘海屏
     *
     * @param activity activity
     * @return 是否是刘海屏
     */
    public static boolean isSpecialScreen(Activity activity) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                final View decorView = activity.getWindow().getDecorView();
                WindowInsets windowInsets = decorView.getRootWindowInsets();
                if (windowInsets != null) {
                    DisplayCutout cutoutDisplay = windowInsets.getDisplayCutout();
                    if (cutoutDisplay != null) {
                        displayCutout = cutoutDisplay;
                        return true;
                    }
                }
            } else {
                // 通过其他方式判断是否有刘海屏  目前官方提供有开发文档的就 小米，vivo，华为（荣耀），oppo
                String manufacturer = Build.MANUFACTURER;
                if (TextUtils.isEmpty(manufacturer)) {
                    return false;
                } else if (manufacturer.equalsIgnoreCase("HUAWEI")) {
                    return hasNotchHw(activity);
                } else if (manufacturer.equalsIgnoreCase("xiaomi")) {
                    return hasNotchXiaoMi(activity);
                } else if (manufacturer.equalsIgnoreCase("oppo")) {
                    return hasNotchOPPO(activity);
                } else if (manufacturer.equalsIgnoreCase("vivo")) {
                    return hasNotchVIVO(activity);
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            MLog.d("", e.getMessage());
            return false;
        }
        return false;
    }

    /**
     * 判断vivo是否有刘海屏
     * https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20180328/20180328152252602.pdf
     */
    private static boolean hasNotchVIVO(Activity activity) {
        try {
            Class<?> c = Class.forName("android.util.FtFeature");
            Method get = c.getMethod("isFeatureSupport", int.class);
            return (boolean) (get.invoke(c, 0x20));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断oppo是否有刘海屏
     * https://open.oppomobile.com/wiki/doc#id=10159
     */
    private static boolean hasNotchOPPO(Activity activity) {
        return activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

    /**
     * 判断xiaomi是否有刘海屏
     * https://dev.mi.com/console/doc/detail?pId=1293
     */
    private static boolean hasNotchXiaoMi(Activity activity) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("getInt", String.class, int.class);
            return (int) (get.invoke(c, "ro.miui.notch", 0)) == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断华为是否有刘海屏
     * https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114
     */
    private static boolean hasNotchHw(Activity activity) {
        try {
            ClassLoader cl = activity.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            return (boolean) get.invoke(HwNotchSizeUtil);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取状态栏的高度
     */
    public static int getStatusBarHeight(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return 0;
        }
        Resources resources = activity.getResources();
        int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelOffset(identifier);
    }

    /**
     * 判断view是否被摄像头遮挡
     */
    public static boolean isViewCovered(Activity activity, View view) {
        //判断是否是异形屏（里面包含Android P以上及以下异形屏的判断）
        if (isSpecialScreen(activity) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            List<Rect> rects = displayCutout.getBoundingRects();
            int rectLfet = rects.get(0).left;
            int rectRight = rects.get(0).right;
            int rectTop = rects.get(0).top;
            int rectBottom = rects.get(0).bottom;
            int[] location = new int[2];
            view.getLocationOnScreen(location); //获取在整个屏幕内的绝对坐标，含statusBar
            int width = view.getWidth();
            int height = view.getHeight();
            int left = location[0];
            int right = location[0] + width;
            int top = location[1];
            int bottom = location[1] + height;
            if (((bottom <= rectBottom && bottom > rectTop) || (top < rectBottom && top >= rectTop))
                    && ((right > rectLfet && right <= rectRight)
                    || (left >= rectLfet && right <= rectRight)
                    || (left >= rectLfet && left < rectRight)
                    || (left < rectLfet && right > rectRight))) {
                return true;
            } else if (((left >= rectLfet && left < rectRight) || (right > rectLfet && right <= rectRight))
                    && ((bottom > rectTop && bottom <= rectBottom)
                    || (top >= rectTop && bottom <= rectBottom)
                    || (top >= rectTop && top < rectBottom)
                    || (top < rectTop && bottom > rectBottom))) {
                return true;
            } else
                return left <= rectLfet && right >= rectRight && top <= rectTop && bottom >= rectBottom;
        }
        return false;
    }
}
